/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.gui.documents.toolbox;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;

import math.Relation;

import common.Relations;
import common.listeners.RelationListener;

import ui.WindowManager;
import ui.gui.documents.DockableDocument;
import ui.gui.documents.IconifiableItem;
import ui.gui.documents.AbstractDocument;

/**
 * 
 * @author stephane
 */
public class Workspace extends DefaultMutableTreeNode implements
		IconifiableItem, RelationListener {
	private static final long serialVersionUID = 1L;

	private String _name = "";

	private static Workspace _instance = null;

	private ArrayList<AbstractDocument> _documents = new ArrayList<AbstractDocument>();

	public static Workspace getInstance() {
		if (_instance == null) {
			_instance = new Workspace();
		}
		return _instance;
	}

	private Workspace() {
		this._name = "Workspace";
		Relations.addRelationListener(this);
	}

	public void Save(String fileName) {
		common.Commands.RunCommand("save-workspace " + fileName, true);
	}

	public void Load(String fileName) {
		common.Commands.RunCommand("load-workspace " + fileName, true);
	}

	/**
	 * @return the _name
	 */
	public String getName() {
		return _name;
	}

	/**
	 * @param name
	 *            the _name to set
	 */
	protected void setName(String name) {
		this._name = name;
	}

	@Override
	public ImageIcon getIcon() {
		return new javax.swing.ImageIcon(getClass().getResource(
				"/resources/workspaceIcon.png"));
	}

	@Override
	public String toString() {
		return getName();
	}

	public void AddDocument(AbstractDocument document) {
		boolean opened = false;
		for (AbstractDocument doc : _documents) {
			if (doc.getFileName().equals(document.getFileName())) {
				opened = true;
			}
		}
		if (!opened) {
			this._documents.add(document);
			this.add(document.getNode());
			WorkspaceExplorer.getInstance().refresh();
			WorkspaceExplorer.getInstance().expandAll();
		}
	}
	

	public void refresh() {
		for (int i = 0; this.children != null && i < this.children.size(); i++) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) this.children
					.get(i);
			node.removeAllChildren();
			for (Relation r : Relations.getRelations()) {
				if (((AbstractDocument) node.getUserObject() != null
						&& ((AbstractDocument) node.getUserObject())
								.getFileName() != null && r.getModel().getAst()
						.getFileName().endsWith(
								((AbstractDocument) node.getUserObject())
										.getFileName()))) {
					DefaultMutableTreeNode relNode = new DefaultMutableTreeNode(
							(IconifiableItem) r);
					node.add(relNode);
				}
			}
		}
	}


	
	public void relationAdded(Relation relation) {
		refresh();
		WorkspaceExplorer.getInstance().expandAll();
	}

	public void relationRemoved(Relation relation) {
		refresh();
		WorkspaceExplorer.getInstance().expandAll();
	}

	public List<AbstractDocument> getDocuments() {
		return Collections.unmodifiableList(_documents);
	}

}
